package com.XiGuaXiaoMe.controller;

import com.XiGuaXiaoMe.dao.userDao;
import com.XiGuaXiaoMe.entity.Users;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class loginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName3,password;
        userDao dao=new userDao();
        int result=0;
        HttpSession session = req.getSession();
        //1.调用请求对象对请求体使用utf-8字符集进行重新编辑
        req.setCharacterEncoding("utf-8");
        //2.调用请求对象读取请求体参数信息
        userName3=req.getParameter("userName3");
        password=req.getParameter("password");
        //3.调用Dao将查询信息推送到数据库服务器上
        result=dao.login(userName3,password);
        //4.调用响应对象，根据验证结果将不同资源文件地址写入到响应头，交给浏览器
        if(result==1)
        {

            Users user = new Users(userName3,password);
            System.out.println(result);
            session.setAttribute("user", user);
            resp.sendRedirect("/MyWeb/Student/userinformation.jsp");
        }else{
            System.out.println(result);

             resp.sendRedirect("/MyWeb/login_error.jsp");
        }
    }

}
